package com.cloudcentral.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudcentral.entity.DetectResult;
import com.cloudcentral.entity.DetectResultBest;
import com.cloudcentral.entity.query.DetectResultBestGenerateQuery;
import com.cloudcentral.feign.ToolkitFeign;
import com.cloudcentral.mapper.DetectResultBestMapper;
import com.cloudcentral.util.Result;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 探测结果优选记录表 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2023-11-23
 */
@Slf4j
@Service
public class DetectResultBestService extends ServiceImpl<DetectResultBestMapper, DetectResultBest> {
  private final ToolkitFeign toolkitFeign;

  @Autowired
  public DetectResultBestService(ToolkitFeign toolkitFeign) {
    this.toolkitFeign = toolkitFeign;
  }

  @Transactional(rollbackFor = {Exception.class})
  @GlobalTransactional(rollbackFor = {Exception.class})
  public Boolean generate(DetectResultBestGenerateQuery query) throws Exception {
    List<List<Object>> row = new ArrayList<>();
    List<Long> resultIds = new ArrayList<>();
    for (int hour = 0; hour < 24; hour++) {
      List<DetectResult> detectResultList = baseMapper.selectResultsByHour(hour, query);
      List<Object> column = new ArrayList<>();
      column.add(hour);
      column.add(StringUtils.join(hour < 10 ? StringUtils.join(0, hour) : hour, ":00:00"));
      column.add(StringUtils.join(hour < 10 ? StringUtils.join(0, hour) : hour, ":59:59"));
      column.addAll(detectResultList.stream().mapToDouble(DetectResult::getFrequency).boxed().toList());
      row.add(column);
      resultIds.addAll(detectResultList.stream().mapToLong(DetectResult::getId).boxed().toList());
    }
    String content = row.stream().map(item -> item.stream().map(Object::toString).collect(Collectors.joining(","))).collect(Collectors.joining("\n"));
    Result<String> result = toolkitFeign.uploadCSV(content);
    if (result.isSuccess()) {
      return save(DetectResultBest.builder()
          .masterId(query.getMasterId())
          .slaveId(query.getSlaveId())
          .url(result.getData())
          .resultIds(resultIds.stream().map(String::valueOf).collect(Collectors.joining(",")))
          .beginTime(query.getBeginDate())
          .endTime(query.getEndDate())
          .build());
    } else return false;
  }

  @Transactional(rollbackFor = {Exception.class})
  @GlobalTransactional(rollbackFor = {Exception.class})
  public Boolean delete(Long id) throws Exception {
    DetectResultBest detectResultBest = getById(id);
    removeById(id);
    toolkitFeign.delete(detectResultBest.getUrl());
    return true;
  }
}
